using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;
using System;
using UnityEngine.SceneManagement;

namespace survivor
{
    public class Global : Architecture<Global>
    {
        //生命值
        public static BindableProperty<int> HP = new BindableProperty<int>(3);
        //经验值
        public static BindableProperty<int> Exp = new BindableProperty<int>(0);
        //金币
        public static BindableProperty<int> Coin = new BindableProperty<int>(0);
        //等级
        public static BindableProperty<int> Level = new BindableProperty<int>(1);
        //攻击力
        public static BindableProperty<float> SimpleAblityDamage = new BindableProperty<float>(1);
        //攻击间隔
        public static BindableProperty<float> SimpleAblityDuration = new BindableProperty<float>(1.5f);
        //波次
        public static BindableProperty<float> CurrentSeconds = new BindableProperty<float>(0);
        //经验掉落概率
        public static BindableProperty<float> ExpPercent = new BindableProperty<float>(0.4f);
        //金币掉落概率
        public static BindableProperty<float> CoinPercent = new BindableProperty<float>(0.1f);

        [RuntimeInitializeOnLoadMethod]
        public static void AutoInit(){
            ResKit.Init();
            UIKit.Root.SetResolution(1920, 1080, 1);

            Global.Coin.Value = PlayerPrefs.GetInt("coin", 0);

            Global.ExpPercent.Value = PlayerPrefs.GetFloat(nameof(ExpPercent), 0.4f);

            Global.CoinPercent.Value = PlayerPrefs.GetFloat(nameof(CoinPercent), 0.1f);

            Global.Coin.Register(coin =>{
                PlayerPrefs.SetInt(nameof(coin), coin);
            });

            Global.ExpPercent.Register(Exp =>{
                PlayerPrefs.SetFloat(nameof(Exp), Exp);
            });

            Global.CoinPercent.Register(Coin =>{
                PlayerPrefs.SetFloat(nameof(Coin), Coin);
            });
        }

        public static void ResetData()
        {
            HP.Value = 3;
            Exp.Value = 0;
            Level.Value = 1;
            CurrentSeconds.Value = 0;
            SimpleAblityDamage.Value = 1;
            SimpleAblityDuration.Value = 1.5f;
            EnemyGenerator.EnemyCount.Value = 0;

            Time.timeScale = 1.0f;
        }

        public static int ExpToNextLevel()
        {
            return Level.Value * 5;
        }

        internal static void GeneratePowerUp(Vector3 pos)
        {
            var random = UnityEngine.Random.Range(0, 1f);

            //经验值
            if(random < ExpPercent.Value){
                DropDownManager.Default.Exp.Instantiate().Position(pos).Show();
            }

            random = UnityEngine.Random.Range(0, 1f);
            if(random < CoinPercent.Value){
                DropDownManager.Default.Coin.Instantiate().Position(pos).Show();
            }
        }

        protected override void Init(){
        }

        public static void Cleanup()
        {
            // 清理UI
            UIKit.CloseAllPanel();
            // 重置数据
            ResetData();
            // 清理其他全局状态
            Time.timeScale = 1.0f;
            
            // 清理UIRoot
            //if (UIKit.Root != null)
            //{
            //    // 确保所有子对象都被销毁
            //    foreach (Transform child in UIKit.Root.transform)
            //    {
            //        GameObject.Destroy(child.gameObject);
            //    }
            //    // 销毁UIRoot本身
            //    GameObject.Destroy(UIKit.Root.gameObject);
            //}
        }
    }
}
